package cvi.com.reader.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.orhanobut.hawk.Hawk;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cvi.com.reader.R;
import cvi.com.reader.activity.ChartsDataActivity;
import cvi.com.reader.activity.RandomReadingActivity;
import cvi.com.reader.activity.WorksDetailsAcitivity;
import cvi.com.reader.activity.WorksDetailsAcitivity2;
import cvi.com.reader.bean.HotBean;
import cvi.com.reader.bean.TextBookBean;
import cvi.com.reader.tool.UtilsOnclickListen;

/**
 * Created by 梅涛 on 2020/7/24.
 */

public class PoetryCollectAdapter extends BaseAdapter {
    private Context context;
    private List<HotBean.RowsBean> dataBeanList;
    private CollectListener collectListener;

    public CollectListener getCollectListener() {
        return collectListener;
    }

    public void setCollectListener(CollectListener collectListener) {
        this.collectListener = collectListener;
    }

    public PoetryCollectAdapter(Context context, List<HotBean.RowsBean> dataBeanList) {
        this.context = context;
        this.dataBeanList = dataBeanList;
    }

    @Override
    public int getCount() {
        return dataBeanList.size();
    }

    @Override
    public Object getItem(int i) {
        return dataBeanList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viHolder = null;
        final HotBean.RowsBean dataBean = dataBeanList.get(i);
        if (view == null) {
            view = LayoutInflater.from(context).inflate(
                    R.layout.item_grid_poetry_collect, null);
            viHolder = new ViewHolder(view);

            view.setTag(viHolder);
        } else {
            viHolder = (ViewHolder) view.getTag();
        }
        viHolder.workName.setText(dataBean.getMaterial_name());
        viHolder.auther.setText(dataBean.getAuthor());
        viHolder.charNum.setText("文章字数:" + dataBean.getWords_count() + "");
        viHolder.readNum.setText("朗读次数:" + dataBean.getClick_times() + "");
        viHolder.secondFloor.getBackground().setAlpha(178);
        viHolder.firstFloor.getBackground().setAlpha(178);
        if (0 == dataBean.getIs_collected()) {
            viHolder.collectBtn.setBackgroundResource(R.drawable.ic_collect_un);
            viHolder.collectBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    collectListener.onCollectClick("1", dataBean.getId());//调用收藏接口

                }
            });
        } else {
            viHolder.collectBtn.setBackgroundResource(R.drawable.ic_collect);
            viHolder.collectBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    collectListener.onCollectClick("0", dataBean.getId());//调用取消收藏接口
                }
            });
        }
        viHolder.rankBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (UtilsOnclickListen.isFastClick()) {
                    Intent intent = new Intent(context, ChartsDataActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putInt("material_id", dataBean.getId());
                    bundle.putString("material_name", dataBean.getMaterial_name());
                    intent.putExtras(bundle);
                    context.startActivity(intent);
                }
            }
        });
        viHolder.readItNow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (UtilsOnclickListen.isFastClick()) {

                Hawk.put("WorksDetails", dataBean);
                Intent intent = new Intent(context, WorksDetailsAcitivity2.class);
                context.startActivity(intent);
            }    }
        });
        return view;
    }

    public interface CollectListener {
        public void onCollectClick(String flag, int id);
    }

    static class ViewHolder {
        @BindView(R.id.collect_btn)
        Button collectBtn;
        @BindView(R.id.work_name)
        TextView workName;
        @BindView(R.id.auther)
        TextView auther;
        @BindView(R.id.first_floor)
        LinearLayout firstFloor;
        @BindView(R.id.read_it_now)
        Button readItNow;
        @BindView(R.id.rank_btn)
        Button rankBtn;
        @BindView(R.id.second_floor)
        LinearLayout secondFloor;
        @BindView(R.id.read_num)
        TextView readNum;
        @BindView(R.id.char_num)
        TextView charNum;
        @BindView(R.id.rl_all)
        RelativeLayout rlAll;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
